#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os

from turbine_opt.data import DATA_SOURCE
from turbine_opt.lib import read_data
from turbine_opt.lib import dispose_blades
from turbine_opt.lib import concat_groups
from turbine_opt.lib import plot_blades
from turbine_opt.lib import split_in_groups
from turbine_opt.lib import random_neighbour_choice
from turbine_opt.lib import cost_function_unbalance
from turbine_opt.lib import temperature
from turbine_opt.lib import acceptance_probability
from turbine_opt.optimization import Simulated_annealing

"""
Command to test the balancing algorithm, and plot results
"""

if __name__ == '__main__':

    # plotting unbalance:
    data_file = os.path.join(DATA_SOURCE, "input-data.csv")
    disk = read_data(data_file)

    print("start preplacing blades")
    alpha = 0.05
    # alpha = 0.01
    # group1, group2 = group_creation(disk, alpha)
    group1, group2 = dispose_blades(disk, alpha)
    disposed = concat_groups(group1, group2)
    print("done preplacing blades")

    disk.loc[:, ["w", "blade", "ht"]] = disposed[["w", "blade", "ht"]]
    plot_blades(disk)

    init_state = disk
    chg_state_func = random_neighbour_choice
    ener_func = cost_function_unbalance
    temp_func = temperature
    max_steps = 500
    accept_proba = acceptance_probability
    Simu = Simulated_annealing(init_state, chg_state_func, ener_func,
                               temp_func, max_steps, accept_proba)
    group1, group2 = split_in_groups(disk)
    res = Simu.optimize()
    plot_blades(res)
